const { obd_collection } = require('../config')
const { db } = require('../config')
const { command } = require('../config')
const _ = command
exports.main = async (event, context) => {
    const data = event;
    loginInfo = data.userInfo;//return loginInfo
    // 根据筛选条件查询obd
    filter = {};
    // 如果请求没有区域网格，则按照token信息
    if (data.hasOwnProperty('region')) {
        filter.region = new RegExp(data.region, "i");
    } else {
        filter.region = loginInfo.region;
    }
    if (data.grid) {
        filter.grid = _.in(data.grid.split(','));
    } else {
        filter.grid = loginInfo.grid;
    }
    if (data.hasOwnProperty('isNewAdd')) {
        if(data.isNewAdd=="是"){
            filter.isNewAdd = data.isNewAdd;
        }else{
            filter.isNewAdd = _.neq("是");
        }
    }
    console.log(filter)
    // return;
    data.staffName && (filter.staffName = new RegExp(data.staffName, 'i'))
    data.fgqmc && (filter.fgqmc = new RegExp(data.fgqmc, 'i'))
    data.staff && (filter.staff = new RegExp(data.staff, 'i'))
    const collection = obd_collection;
    const count = await db.collection(collection).where(filter).count();
    page = data.page;
    pageSize = data.pageSize;
    const lists = await db.collection(collection).where(filter).skip(page * pageSize).limit(pageSize).get();
    // console.log(lists)
    return { total: count.total, data: lists.data, page, pageSize };
}